#include <stdio.h>


#define ListSize 10
typedef int DataType;

typedef struct {
    DataType data[ListSize];
    int length;
} ArrayList;

// 依次插入一个元素。
void add(ArrayList *list, DataType value) {

}

/*
 * 在线性表I个位置插入一个数据
 * index 从1开始 到长度位置结束
 * 只能插入到length之前，不能提前往后面插入
 */
void addIndex(ArrayList *list, int index, DataType value) {

    if (index < 1 || index > list->length + 1) {
        printf("Index {%d} out of Error\n", index);
        return;
    }

    if (list->length >= ListSize) {
        printf("Out Of List! \n");
        return;
    }

    for (int i = list->length - 1; i >= index - 1; i--) {
        list->data[i + 1] = list->data[i];
    }

    list->data[index - 1] = value;
    list->length++;
}

/**
 * 删除指定位置的元素，其余向前移动一位
 * @param list
 * @param index 从1开始到length长度
 * @return
 */
DataType deleteList(ArrayList *list, int index) {

    if (index < 1 || index > list->length) {
        printf("index error {%d} \n", index);
        return 0;
    }

    int del = list->data[index - 1];

    for (int i = index; i <= list->length; i++) {
        list->data[i - 1] = list->data[i];
    }
    list->length--;

    return del;
}


void converts(ArrayList *list) {

    int num = list->length / 2;

    for (int i = 0; i < num; i++) {
        int item = list->data[i];
        list->data[i] = list->data[list->length - i - 1];
        list->data[list->length - i - 1] = item;
    }

}


ArrayList init() {
    ArrayList list;

    list.length = 0;
    for (int i = 0; i < ListSize; i++) {
        list.data[i] = 0;
    }
    return list;
}

void toString(ArrayList *list) {

    printf("[");
    for (int i = 0; i < ListSize; i++) {
        printf("%d", list->data[i]);

        if (i < ListSize - 1) {
            printf(",");
        }
    }
    printf("]\n");

}

int main() {
    printf("Hello, World!\n");

    ArrayList list = init();
    ArrayList *arrayList = &list;

    addIndex(arrayList, 1, 1);
    addIndex(arrayList, 2, 2);
    addIndex(arrayList, 3, 3);
    addIndex(arrayList, 4, 4);
    addIndex(arrayList, 5, 5);
    addIndex(arrayList, 6, 6);
    addIndex(arrayList, 3, 33);

    toString(arrayList);

    int delNum = deleteList(arrayList, 4);
    printf("delete num ={%d} \n", delNum);

    toString(arrayList);

    converts(arrayList);
    toString(arrayList);

    return 0;
}
